package com.puge.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.puge.vod.service.VodService;
import com.puge.vod.utils.ConstantVodUtils;
import com.puge.vod.utils.InitVodCilent;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * @author by sun
 * @date 2021/6/24.12:57
 */
@Component
@Service
public class VodServiceImpl implements VodService {

    //    上传视频到阿里云
    @Override
    public String uploadVideiAly(MultipartFile file) {
        try{
            //fileName:上传文件原始名称
            String fileName = file.getOriginalFilename();
            //title：上传之后的显示名称
            String title = fileName.substring(0,fileName.lastIndexOf("."));
            //inputStream：
            InputStream inputStream = file.getInputStream();
            System.out.println("++++++++++++++这个视频开始添加了++++++++++++++++");

            UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET,title,fileName,inputStream);
            System.out.println(ConstantVodUtils.ACCESS_KEY_ID);
            System.out.println(ConstantVodUtils.ACCESS_KEY_SECRET);
            /* ECS部署区域*/
            // request.setEcsRegionId("cn-shanghai");
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);

            String videoId = null;
            if (response.isSuccess()) {
                System.out.println("++++++++++++++这个视频被添加了++++++++++++++++");
                videoId = response.getVideoId();
            } else {
                /* 如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因 */
                videoId = response.getVideoId();
                System.out.println("这个视频被添加了 ======");
                System.out.print("VideoId=" + response.getVideoId() + "\n");
                System.out.print("ErrorCode=" + response.getCode() + "\n");
                System.out.print("ErrorMessage=" + response.getMessage() + "\n");
            }
            return videoId;
        }catch (Exception e){
            //打印异常输出
            e.printStackTrace();
            return null;
        }
    }

    //一次删除多个阿里云视频的方法
    @Override
    public void removeMoreAlyVideo(List videoIdList) {
        try {
            //    初始化对象
            DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //   创建删除对象request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //调用  阿帕奇  里面的StringUtils将数组以用逗号隔开的字符串形式输出
            String join = StringUtils.join(videoIdList.toArray(), ",");
            //    向request设置视频Id
            request.setVideoIds(join);

            //    调用初始化对象的方法实现删除
            client.getAcsResponse(request);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("删除失败");
            throw new PuGeException(20001,"删除视频失败");
        }

    }
}

